package com.lfk.justweengine.Utils.tools;

import ohos.agp.colors.RgbColor;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

/**
 * Created by liufengkai on 15/11/10.
 */
public class ImageHelper {

    /**
     * 设置图片的色调\饱和度\亮度
     *
     * @param pm
     * @param hue        色调
     * @param saturation 饱和度
     * @param lum        亮度
     * @return
     */
    public static PixelMap handleImageEffect(PixelMap pm,
                                             float hue,
                                             float saturation,
                                             float lum) {
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = PixelFormat.ARGB_8888;
        PixelMap pmp = PixelMap.create(pm, options);

        Canvas canvas = new Canvas(new Texture(pmp));
        Paint paint = new Paint();

        ColorMatrix hueMatrix = new ColorMatrix();

        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);

        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum, lum, lum, 1);

        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);

        paint.setColorMatrix(imageMatrix);
        PixelMapHolder pixelMapHolder = new PixelMapHolder(pm);
        canvas.drawPixelMapHolder (pixelMapHolder, 0, 0, paint);
        return pmp;
    }

    public static PixelMap handleImagePixelsOldPhoto(PixelMap pm) {
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = PixelFormat.ARGB_8888;
        PixelMap pmp = PixelMap.create(pm, options);
        int width = pm.getImageInfo().size.width;
        int height = pm.getImageInfo().size.height;
        int color = 0;
        int r, g, b, a, r1, g1, b1;

        int[] oldPx = new int[width * height];
        int[] newPx = new int[width * height];

        ohos.media.image.common.Rect rect = new ohos.media.image.common.Rect(0, 0, width, height);

        pm.readPixels(oldPx, 0, pm.getImageInfo().size.width, rect);

        for (int i = 0; i < width * height; i++) {
            color = oldPx[i];
            RgbColor rgbColor = new RgbColor(color);
            a = Color.alpha(color);
            r = rgbColor.getRed();
            g = rgbColor.getGreen();
            b = rgbColor.getBlue();

            r1 = (int) (0.393 * r + 0.769 * g + 0.189 * b);
            g1 = (int) (0.349 * r + 0.686 * g + 0.168 * b);
            b1 = (int) (0.272 * r + 0.534 * g + 0.131 * b);

            if (r1 > 255) {
                r1 = 255;
            }
            if (g1 > 255) {
                g1 = 255;
            }
            if (b1 > 255) {
                b1 = 255;
            }

            newPx[i] = Color.argb(a, r1, g1, b1);
        }
        pmp.writePixels(newPx, 0, width, rect);
        return pmp;
    }

    public static PixelMap handleImagePixelsRelief(PixelMap pm) {
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = PixelFormat.ARGB_8888;
        PixelMap pmp = PixelMap.create(pm, options);
        int width = pm.getImageInfo().size.width;
        int height = pm.getImageInfo().size.height;
        int color = 0, colorBefore = 0;
        int a, r, g, b;
        int r1, g1, b1;

        int[] oldPx = new int[width * height];
        int[] newPx = new int[width * height];

        ohos.media.image.common.Rect rect = new ohos.media.image.common.Rect(0, 0, width, height);

        pm.readPixels(oldPx, 0, pm.getImageInfo().size.width, rect);
        for (int i = 1; i < width * height; i++) {
            colorBefore = oldPx[i - 1];
            RgbColor rgbColorBefore = new RgbColor(colorBefore);
            a = Color.alpha(colorBefore);
            r = rgbColorBefore.getRed();
            g = rgbColorBefore.getGreen();
            b = rgbColorBefore.getBlue();

            color = oldPx[i];
            RgbColor rgbColor = new RgbColor(color);
            r1 = rgbColor.getRed();
            g1 = rgbColor.getGreen();
            b1 = rgbColor.getBlue();

            r = (r - r1 + 127);
            g = (g - g1 + 127);
            b = (b - b1 + 127);
            if (r > 255) {
                r = 255;
            }
            if (g > 255) {
                g = 255;
            }
            if (b > 255) {
                b = 255;
            }
            newPx[i] = Color.argb(a, r, g, b);
        }
        pmp.writePixels(newPx, 0, width, rect);
        return pmp;
    }
}
